﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace YoUI.Core
{
    /// <summary>
    /// 键值
    /// </summary>
    public class KeyVaules
    {
        private string _Key;
        private object _Values;

        public string Key
        {
            get { return _Key; }
        }

        public object Values
        {
            get { return _Values; }
        }

        public KeyVaules(string key,object value)
        {
            _Key = key;
            _Values = value;
        }
    }

    public class YoMessageCenter
    {
        public delegate void DelMessage(KeyVaules kv);

        public static Dictionary<string, DelMessage> _DicMessage = new Dictionary<string, DelMessage>();

        /// <summary>
        /// 添加监听
        /// </summary>
        /// <param name="msgType">消息类型</param>
        /// <param name="handler">处理</param>
        public static void AddListenner(string msgType,DelMessage handler)
        {
            if (!_DicMessage.ContainsKey(msgType))
            {
                _DicMessage.Add(msgType, null);
            }

            _DicMessage[msgType] += handler;
        }

        /// <summary>
        /// 移除监听
        /// </summary>
        /// <param name="msgType">消息类型</param>
        /// <param name="handler">处理</param>
        public static void RemoveListener(string msgType,DelMessage handler)
        {
            if (_DicMessage.ContainsKey(msgType))
            {
                _DicMessage[msgType] -= handler;
            }
        }

        /// <summary>
        /// 清空所有监听
        /// </summary>
        public static void ClearAllListener()
        {
            if(_DicMessage != null)
            {
                _DicMessage.Clear();
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msgType">消息类型</param>
        /// <param name="kv">内容</param>
        public static void SendMessage(string msgType,KeyVaules kv)
        {

            DelMessage del;

            if(_DicMessage.TryGetValue(msgType,out del))
            {
                if(del != null)
                {
                    del(kv);
                }
            }
        }

        public static void SendMessage(string msgType,string k,object v)
        {
            KeyVaules kv = new KeyVaules(k, v);
            SendMessage(msgType, kv);
        }


    }
}
